<?php
    function ShowSeries($admin_id)
    {
        require_once('Database/Database.php');
        $db = new Database();
        $db->Connect();
        $data = $db->SelectColumns('Admins', 'Series', 'ID_Admin = \'' . $admin_id . '\'');

        if($data)
        {
            $temp = explode(',' , $data[0]->Series);
            $series = array();
            foreach($temp as $t)
            {
               $d = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = \'' . $t . '\'' );
               $series[] = $d->Shortcut;
            }
            $series_names = implode(', ', $series);
            echo ($series_names != '') ? $series_names : '';
        }
        $db->Disconnect();
    }

    function ShowLeagues($admin_id)
    {
        require_once('Database/Database.php');
        $db = new Database();
        $db->Connect();
        $data = $db->SelectColumns('Admins', 'Leagues', 'ID_Admin = \'' . $admin_id . '\'');

        if($data)
        {
            $temp = explode(',' , $data[0]->Series);
            $leagues = array();
            foreach($temp as $t)
            {
               $d = $db->SelectColumns('Leagues', 'Shortcut', 'ID_League = \'' . $t . '\'' );
               $leagues[] = $d->Shortcut;
            }
            $league_names = implode(', ', $leagues);
            echo ($league_names != '') ? $league_names : '';
        }
        $db->Disconnect();
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admins | Ranking - ALPHA</title>
    </head>
    <body>
        <div id="header">Admins</div>
        <div id="manage_admins">
            <table>
                <tr>
                    <td>
                        <a href="add_admin.php" target="_blank"><img src="Icons/add_user.png" /></a>
                    </td>   
                </tr>
            </table>
        </div>
        <div id="main">
            <table>
                <tr>
                    <td>Name</td>
                    <td>Super-Admin</td> <!-- Überschreibt alles. Rechte für jede Liga und Serie.. -->
                    <td>Ligen</td> <!-- Rechte umfassen alle Serien einer Liga.. -->
                    <td>Serien</td> <!-- Nur Rechte in einzelnen Serien.. -->
                    <td>Ändern</td>
                    <td>Entfernen</td>
                </tr>
                <?php
                  require_once('Database/Database.php');
                  $db = new Database();
                  $db->Connect();

                  try
                  {
                      $data = $db->Select('Admins');
                  }
                  catch(Exception $ex)
                  {
                     echo 'No admins found.';
                     echo $ex;
                  }
                  $db->Disconnect();

                  if($data != null)
                  {
                    foreach($data as $d)
                    {
                        // Joomla-Link
                        $name_string = '<a href="../index.php?option=com_users&view=profile&user_id=' . $d->JID .
                                '" target="_blank">' . htmlentities($d->Name) . '</a>';
                        echo '<tr>';
                        echo '<td>' . $name_string . '</td>';
                        if($d->Super_Admin == 1)
                        {
                            echo '<td>Ja</td>';
                            echo '<td>alle</td>';
                            echo '<td>alle</td>';
                        }
                        else
                        {
                            echo '<td>Nein</td>';
                            echo '<td>' . ShowLeagues($d->ID_Admin) . '</td>';
                            echo '<td>' . ShowSeries($d->ID_Admin) . '</td>';
                        }
                        echo '<td><a href="edit_admin.php?id=' . $d->ID_Admin . '&name=' . htmlentities($d->Name) . '" target="_blank"><img src="Icons/edit.png" /></a></td>';
                        echo '<td><a href="remove_admin.php?id=' . $d->ID_Admin . '&name=' . htmlentities($d->Name) . '" target="_blank"><img src="Icons/remove_user.png" /></a></td>';
                        echo '</tr>';
                    }
                  }
                  else
                  {
                      echo 'No admins.';
                  }
                 ?>
            </table>
        </div>
        <div id="footer"></div>
    </body>
</html>